<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8"/>
    <title>列表</title>
    <link href="__STATIC__/component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <div style="padding-left: 15px;">
            模型标识：{:widget('model')->get($model_id)}
            &nbsp;&nbsp;<span class="layui-font-blue">表字段不存在：表不存在-模型-生成表，表存在-编辑-更新，并生成MVC！</span>
        </div>
        <table class="layui-hide" id="currentTable" lay-filter="currentTable"
               data-auth-add="{:check_auth('add')}"
               data-auth-edit="{:check_auth('edit')}"
               data-auth-delete="{:check_auth('del')}"
               data-auth-modify="{:check_auth('multi')}"
               data-auth-import="{:check_auth('import')}"
               data-auth-export="{:check_auth('export')}"
               data-auth-preset="{:check_auth('preset')}"
        ></table>
    </div>
</div>

<script src="__STATIC__/component/layui/layui.js"></script>
<script src="__STATIC__/component/pear/pear.js"></script>
<script>
    layui.use(['aphpTable', 'aphp', 'table'], function () {
        var aphpTable = layui.aphpTable;
        var $ = layui.$;
        var aphp = layui.aphp;
        var table = layui.table;

        var init = {
            table_elem: '#currentTable',
            table_render_id: 'currentTable',
            add_url: "{:url('add')}?model_id={$model_id}",
            edit_url: "{:url('edit')}?model_id={$model_id}",
            delete_url: "{:url('del')}?model_id={$model_id}",
            modify_url: "{:url('multi')}?model_id={$model_id}",
            import_url: "{:url('import')}?model_id={$model_id}",
            export_url: "{:url('export')}?model_id={$model_id}",
            preset_url: "{:url('preset')}?model_id={$model_id}",
        };

        aphpTable.render({
            init: init,
            url: "{:url('index')}?model_id={$model_id}",
            toolbar: ['refresh_table', 'add', 'enable', 'disable', 'delete',[{
                title: '导出',
                text: '导出',
                class: 'layui-btn layui-btn-normal layui-btn-sm',
                icon: 'layui-icon layui-icon-export',
                method: 'none',
                auth: 'export',
                extend: 'data-event="export"',
            }],[{
                title: '导入',
                text: '导入',
                class: 'layui-btn layui-bg-purple layui-btn-sm',
                icon: 'layui-icon layui-icon-upload-drag',
                url: init.import_url,
                method: 'open',
                auth: 'import',
            }]],
            search: false,
            cols: [[
                { type: 'checkbox'},
                { title: '排序', field: 'sort', search: false, edit: 'text'},
                { title: 'ID', field: 'id' },
                { title: '标题', field: 'field_title', width: 120 },
                { title: '名称', field: 'field_name', width: 120 },
                { title: '类型', field: 'field_type', templet: '<div>{{d.field_type}}({{d.field_length}})</div>'},
                { title: '默认值', field: 'field_value'},
                //{ title: '表注释', field: 'field_comment'},
                { title: '表单类型', field: 'form_type'},
                { title: '表字段', field: 'is_table_field', templet: aphpTable.formatter.flag, selectList: {0: '不存在', 1: '存在'} },
                { title: '状态', field: 'status', align: 'center', unresize: true, templet: aphpTable.formatter.switch },
                { title: '操作', align: 'center', width: 160, templet: aphpTable.formatter.tool, operat: ['edit',[{
                        title: '存入预设',
                        text: '',
                        class: 'layui-btn layui-btn-sm',
                        icon: 'layui-icon layui-icon-star',
                        url: init.preset_url,
                        method: 'request',
                        auth: 'preset',
                        confirm: true,
                    }],'delete']}
            ]],
            page: {}
        });
        aphpTable.bindEvent();

        $(document).on('click', "[data-event='export']", function() {
            var data = table.checkStatus(init.table_render_id).data;
            var ids = '';
            var msg = '确认要导出吗？注意：只会导出启用的字段！';
            var url = init.export_url;
            if(data.length > 0){
                for(let i = 0;i<data.length;i++){
                    ids += data[i].id+",";
                }
                ids = ids.slice(0, -1);
                msg = '确认要导出以下ID的字段吗？<br/>' + ids;
                url = url + '&ids=' + ids;
            }
            layer.confirm(msg, {icon: 3, title:'提示'}, function (index) {
                layer.close(index);
                window.location.href = url;
            });
        });

    });
</script>
</body>
</html>